﻿#region << 文 件 说 明 >>
/*----------------------------------------------------------------
// 文件名称：AddProjectViewModel
// 创 建 者：杨程
// 创建时间：2021/9/10 16:58:58
// 文件版本：V1.0.0
// ===============================================================
// 功能描述：
//		
//
//----------------------------------------------------------------*/
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using Newtonsoft.Json;
using V.CodeGenerator.CreateCodes;
using V.CodeGenerator.CreateCodes.Model;
using V.CodeGenerator.DataAccess;
using V.CodeGenerator.Model;
using Vampirewal.Core.Interface;
using Vampirewal.Core.IoC;
using Vampirewal.Core.SimpleMVVM;
using Vampirewal.Core.WpfTheme.WindowStyle;

namespace V.CodeGenerator.ViewModel
{
    [VampirewalIoCRegister(ViewModelKey.AddProjectViewModel, RegisterType.ViewModel)]
    public class AddProjectViewModel : BillVM<ProjectPoco>
    {
        private IDialogMessage Dialog;
        private ILogger Log;
        private IValidationService ValidationService { get; set; }

        public AddProjectViewModel(IDialogMessage dialog, IDataContext _dc, ILogger log,IAppConfig appConfig,IValidationService validation) : base(_dc, appConfig)
        {
            //构造函数
            Dialog = dialog;
            Log = log;
            ValidationService = validation;

            //Entity.SaveFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
        }

        public override void InitData()
        {
            Title = "新建工程";

        }

        public override void PassData(object obj)
        {

        }


        public override object GetResult()
        {
            return Entity;
        }
        #region 属性
        
        #endregion

        #region 公共方法

        #endregion

        #region 私有方法

        #endregion

        #region 命令

        public RelayCommand SelectSaveFolderCommand => new RelayCommand(() =>
        {
            //TODO 等后面自己做一个文件夹选择框
            FolderBrowserDialog fb = new FolderBrowserDialog();
            
            if (fb.ShowDialog()== DialogResult.OK)
            {
                Entity.SaveFolderPath = fb.SelectedPath;
            }
        });

        /// <summary>
        /// 提交命令
        /// </summary>
        public RelayCommand SubmitCommand => new RelayCommand(() =>
        {
            if (!ValidationService.IsValidation(Entity))
            {
                Dialog.ShowPopupWindow($"{string.Join("\r\n", ValidationService.ErrorList)}", (Window)View, MessageType.Error);
                return;
            }

            try
            {
                //将工程保存进数据库
                
                #region 为该工程添加顶层明细
                //ModuleFunctionPoco dtl = new ModuleFunctionPoco()
                //{
                //    ProjectId = Entity.BillId,
                //    ParentId = Guid.Empty.ToString(),
                //    moduleFunctionType = ModuleFunctionType.Top,
                //    PocoName = Entity.ProjectName
                //};

                //DC.AddEntity(dtl);
                #endregion

                

                DoAdd();


                Log.DebugLog(JsonConvert.SerializeObject(Entity), "新建工程模块");


                Dialog.ShowPopupWindow("新建工程成功！", (Window)View, MessageType.Successful);

                DC.Client.CommitTran();
                ((Window)View).Close();
            }
            catch (Exception ex)
            {
                DC.Client.RollbackTran();
                Dialog.ShowPopupWindow($"新建工程失败！错误信息：{ex}", (Window)View, MessageType.Error, true);
                Log.WriteLog(ex.ToString(), Vampirewal.Core.Models.LoggerType.Error, "新建工程模块");
            }
        });

        
        #endregion
    }
}
